﻿/* 
 *  <copyright file="TriggerOnCollisionCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using SobrietyEngine.Common;
using SobrietyEngine.Actor;
using SobrietyEngine.Components;
using SobrietyEngine.Components.Physics;
using SobrietyEngine.Components.Physics.OnCollision;
using SobrietyEngine.Logging;
using SobrietyEngine.Guids;
using SobrietyEngine.Events.Component;

using FarseerPhysics;
using FarseerPhysics.Collision;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;

namespace SobrietyEngine.Components.Physics.OnCollision
{
    public class TriggerOnCollisionCmp : OnCollisionBase
    {
        ITrigger trigger;

        public TriggerOnCollisionCmp()
            : base()
        {
        }

        public override bool InitComponent()
        {
            if (!base.InitComponent())
                return false; //delay

            trigger = Actor.Get<ITrigger>();
            if (trigger == null)
            {
                Logger.GetSingleton().WriteError(this, "Didnt find tigger component");
                return false;//delay
            }

            if (trigger.IsPressureSwitch)
                this.EnableSeperationResponse = true;

            return true;
        }

        override public bool OnCollision(Fixture f1, Fixture f2, Contact contact)
        {
            //filter out actors we dont want trigger on
            IActor other = (IActor)f2.Body.UserData;

            if (trigger.IsPressureSwitch)
            {
                trigger.ActorsOnTrigger.Add(other);
                if (trigger.ActorsOnTrigger.Count > 1)
                {
                    if (!trigger.MultiTrigger)
                        return true;
                }
            }

            //trigger
            trigger.Trigger(true, other.Guid);

            

            return true;
        }

        public override void OnSeperation(Fixture fixtureA, Fixture fixtureB)
        {
            //seperate out actors we dont care about
            IActor other = (IActor)fixtureB.Body.UserData;

            if (trigger.IsPressureSwitch)
            {
                trigger.ActorsOnTrigger.Remove(other);
                if (trigger.ActorsOnTrigger.Count > 0)
                    return; //dont trigger the release event until nobodys on this trigger
            }

            //reset our trigger value
            trigger.Trigger(false, other.Guid);
        }

   
    }
}
